package cn.com.hhrcw.utils;


import cn.hutool.core.lang.Assert;

import java.math.BigDecimal;
import java.text.DecimalFormat;


/**
 * 金额元分之间转换工具类
 * https://gitee.com/gtyw_yanfa/JinEYuanYuFenZhiJianZhuanHuanGongJuLei/blob/master/AmountUtils.java#
 *
 * @author lixingxing
 */
public class AmountUtils {

    /**
     * 金额为分的格式
     */
    public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";

    private AmountUtils() {}

    /**
     * 三位分级法
     * 将分为单位的转换为元并返回金额格式的字符串 （除100） ,并 /每三位以逗号进行分隔。
     *
     * @param amount
     * @return
     * @throws Exception
     */
    public static String changeF2Y23(Long amount) {
        if (null == amount) {
            return "无";
        }
        String value = new DecimalFormat("#,###.00").format(amount / 100.0);
        return value.startsWith(".") ? 0 + value : value;
    }

    /**
     * 将分为单位的转换为元并返回金额格式的字符串 （除100）
     *
     * @param amount
     * @return
     * @throws Exception
     */
    public static String changeF2Y(Long amount) {
        if (null == amount) {
            return "无";
        }

        int flag = 0;
        String amString = amount.toString();
        boolean b = amString.charAt(0) == '-';
        boolean b1 = amString.length() == 2;
        if (b) {
            flag = 1;
            amString = amString.substring(1);
        }
        StringBuilder result = new StringBuilder();
        if (amString.length() == 1) {
            result.append("0.0").append(amString);
        } else if (b1) {
            result.append("0.").append(amString);
        } else {
            String intString = amString.substring(0, amString.length() - 2);
            for (int i = 1; i <= intString.length(); i++) {
                if ((i - 1) % 3 == 0 && i != 1) {
                    result.append(",");
                }
                result.append(intString.charAt(intString.length() - i));
            }
            result.reverse().append(".").append(amString.substring(amString.length() - 2));
        }
        if (flag == 1) {
            return "-" + result;
        } else {
            return result.toString();
        }
    }

    /**
     * 将分为单位的转换为元 （除100）
     *
     * @param amount
     * @return
     * @throws Exception
     */
    public static String changeF2Y(String amount) {
        Assert.isTrue(amount.matches(CURRENCY_FEN_REGEX), "金额格式有误");
        return BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)).toString();
    }

    /**
     * 将元为单位的转换为分 （乘100）
     *
     * @param amount
     * @return
     */
    public static String changeY2F(Long amount) {
        return BigDecimal.valueOf(amount).multiply(new BigDecimal(100)).toString();
    }

    /**
     * 将元为单位的转换为分 替换小数点，支持以逗号区分的金额
     *
     * @param amount
     * @return
     */
    public static String changeY2F(String amount) {
        //处理包含, ￥ 或者$的金额
        String currency = amount.replaceAll("[$￥,]", "");
        int index = currency.indexOf(".");
        int length = currency.length();
        Long amLong;
        boolean b = length - index >= 3;
        boolean b1 = length - index == 2;
        if (index == -1) {
            amLong = Long.valueOf(currency + "00");
        } else if (b) {
            amLong = Long.valueOf((currency.substring(0, index + 3)).replace(".", ""));
        } else if (b1) {
            amLong = Long.valueOf((currency.substring(0, index + 2)).replace(".", "") + 0);
        } else {
            amLong = Long.valueOf((currency.substring(0, index + 1)).replace(".", "") + "00");
        }
        return amLong.toString();
    }
}